<template>
  <div>
    <search-form @search="getTableData" @reset="clearSearch">
      <el-col :span="8" :xs="24" v-show="$permiss[1378]">
        <el-form-item :label="$t('所属租户')">
          <el-select style="width: 100%" v-model="page.tenantId" @change="tenantChange">
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1687]" >
        <el-form-item :label="$t('APP名称')">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1377]">
        <el-form-item :label="$t('客群名称')">
          <el-select style="width: 100%" v-model="page.guestGroupId">
            <el-option v-for="item in guestGroupList" :key="item.guestGroupId" :label="item.guestGroupName"
                       :value="item.guestGroupId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1612]">
        <el-form-item :label="$t('经理名称')">
          <el-select style="width: 100%"
                     v-model="page.managerId" clearable filterable @change="managerChange">
            <el-option v-for="item in managerList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1613]">
        <el-form-item :label="$t('提醒员工')">
          <el-select style="width: 100%" v-model="page.adminUserId" clearable filterable>
            <el-option v-for="item in remindUser" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('日期')">
          <el-date-picker
              v-model="page.timeValue"
              style="width: 100%"
              type="daterange"
              :clearable="false"
              range-separator="~"
              unlink-panels
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始时间')"
              :end-placeholder="$t('结束时间')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </search-form>
    <el-card shadow="never" style="margin-top: 10px">
      <Table :table-column="tableColumn" :loading="loading" :table-data="tableData" :page="false" max-height="700"/>
    </el-card>
  </div>
</template>

<script>
import {remindRealTimeWeekList, guestGroupList, remindRealTimeWeekTotal, guestGroupBindList} from '@/api/postLoan'
import {dateFormatSection, defaultTimeDate} from "@/utils/tools";
import {selectProduct, selectTenant} from "@/api/system";

export default {
  name: "reminderReportWeekly",
  data() {
    return {
      page: {
        timeValue: defaultTimeDate(1)
      },
      guestGroupList: [],
      exportsLoading: false,
      tenantList: [],
      collectionUser: [],
      tableData: [],
      tableColumn: [
        {label: this.$t('排名'), prop: 'rownum', align: 'center', minWidth: 100, fixed: 'left'},
        {label: this.$t('客群'), prop: 'guestGroupName', align: 'center', minWidth: 100},
        {label: this.$t('系统账号'), prop: 'accountNumber', align: 'center', minWidth: 100},
        {label: this.$t('用户姓名'), prop: 'adminUserName', align: 'center', minWidth: 100},
        {label: this.$t('分配'), prop: 'total', align: 'center', minWidth: 100},
        {label: this.$t('待还款'), prop: 'totalRepaid', align: 'center', minWidth: 100},
        {label: this.$t('已还款'), prop: 'totalAlready', align: 'center', minWidth: 100},
        {label: this.$t('展期'), prop: 'totalExtension', align: 'center', minWidth: 100},
        {label: this.$t('已结束'), prop: 'totalEnd', align: 'center', minWidth: 100},
        {label: this.$t('还款实还率'), prop: 'alreadyRate', align: 'center', minWidth: 100},
        {label: this.$t('综合率'), prop: 'rate', align: 'center', minWidth: 100},
        {label: this.$t('当日复借率'), prop: 'dayRate', align: 'center', minWidth: 100},
      ],
      loading: false,
      productList:[]
    }
  },
  async mounted() {
    const p2 = await selectTenant().then(res => {
      this.tenantList = res.data
      if (this.tenantList.length > 0) this.$set(this.page, 'tenantId', this.tenantList[0].adminUserId)
    })
    // 获取客群名称
    const p1 = await guestGroupList({
      pageNum: 1,
      pageSize: 1000,
      type: 0,
      tenantId: this.page.tenantId
    }).then(res => {
      const {list} = res.data
      this.guestGroupList = list || []
      if (this.guestGroupList.length > 0) this.$set(this.page, 'guestGroupId', this.guestGroupList[0].guestGroupId)
    })
    Promise.all([p1, p2]).then(res => {
      this.getTableData()
    })
    //  提醒员
    this.$permiss[1613] && guestGroupBindList({
      pageSize: 100,
      adminRole: 9,
    }).then(res => {
      this.remindUser = res.data.list
    })
    // 经理姓名
    this.$permiss[1612] && guestGroupBindList({
      adminRole: 8,
    }).then(res => {
      this.managerList = res.data.list
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
  },
  methods: {
    async getTableData() {
      const [startTime, endTime] = dateFormatSection(this.page.timeValue)
      const params = {
        ...this.page,
        startTime, endTime
      }
      delete params.timeValue
      this.loading = true
      const res = await remindRealTimeWeekList(params)
      if (res.code === 1) {
        this.tableData = res.data || []
        await this.remindRealTimeWeekTotal(params)
        this.loading = false
      } else {
        this.loading = false
      }
    },
    async remindRealTimeWeekTotal(params) {
      const res = await remindRealTimeWeekTotal(params)
      if (res.code === 1) {
        if (this.tableData.length > 1) {
          this.tableData.unshift({...res.data, rownum: this.$t('合计')})
        }
      }
    },
    managerChange(value) {
      //  提醒员
      this.$permiss[1613] && guestGroupBindList({
        pageSize: 100,
        adminRole: 9,
        managerId: value
      }).then(res => {
        this.$set(this.page, 'adminUserId', null)
        this.remindUser = res.data.list
      })
    },
    tenantChange(value) {
      guestGroupList({
        pageNum: 1,
        pageSize: 1000,
        type: 0,
        tenantId: value
      }).then(res => {
        const {list} = res.data
        this.guestGroupList = list || []
      })
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
      })
    },
    clearSearch() {
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>
